package com.power.util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * ClassName:TimeUtil
 * Package:com.abrs.util
 * Description:
 *
 * @date:2019/12/12 11:36
 * @author:zhangqiang
 */
public class TimeUtil {

    /**
     * 获取当天剩余时间秒数
     *
     * @param date
     * @return
     */
    public static long leftTimeSecondCurrentDay(Date date) {
        String dateTime = new SimpleDateFormat("yyyyMMddHHmmss").format(date);
        String subDateHour = dateTime.substring(8, 10);
        String subDateMinute = dateTime.substring(10, 12);
        String subDateSecond = dateTime.substring(12, 14);
        return 24 * 3600 - Integer.valueOf(subDateHour) * 3600 - Integer.valueOf(subDateMinute) * 60 - Integer.valueOf(subDateSecond);
    }

    /**
     * 根据给定的日期与天数返回某一日期
     *
     * @param date：给定日期
     * @param days：天数(正数返回给定日期之前，负数返回给定日期之后)
     * @return
     */
    public static Date returnSomeDayAccordingGivenDate(Date date, Integer days) {
        return new Date(date.getTime() - days * 24 * 3600 * 1000L);
    }

    /**
     * 获取两个日期之间的间隔小时数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static double getIntervalHours(Date date1, Date date2) {
        return Math.abs((date1.getTime()-date2.getTime())/1000/3600);
    }
}
